// https://umijs.org/config/
import { defineConfig } from 'umi';
import { SAAS_URL_OBJ } from './urlConfig';
import proxy from './proxy';
import routes from './routes';
import { chainWebpack } from './plugin.config';
import { MICRO_CIG } from './micro';

const { NODE_ENV, CLIENT_ENV, TERMINAL_ENV } = process.env;
const BASE_HASH = '/saas/';
const IS_PRODUCTION = NODE_ENV === 'production';
const SAAS_URL = () => SAAS_URL_OBJ[CLIENT_ENV] || SAAS_URL_OBJ.release;

const defineConf = {
  externals: {
    'echarts': 'echarts',
  },
  scripts: [
    'https://10.4.6.209:8083/echarts.min.js',
  ]
};

if (IS_PRODUCTION) {
  defineConf.chunks = ['vendors', 'antdesigns', 'umi'];
  defineConf.externals = {
    'jspdf': 'jspdf',
    'react': 'React',
    'antd': 'antd',
    'react-dom': 'ReactDOM',
    'echarts': 'echarts',
  };
  defineConf.scripts = [
    'https://10.4.6.209:8083/react.production.min.js',
    'https://10.4.6.209:8083/react-dom.production.min.js',
    'https://10.4.6.209:8083/react-router-dom.min.js',
    'https://10.4.6.209:8083/antd.min.js',
    'https://10.4.6.209:8083/echarts.min.js',
  ];
}

export default defineConfig({
  ...defineConf,
  base: `${BASE_HASH}`,
  publicPath: IS_PRODUCTION ? `${SAAS_URL()}${BASE_HASH}` : '/',
  antd: {},
  chainWebpack: chainWebpack,
  define: {
    CLIENT_ENV: CLIENT_ENV,
    TERMINAL_ENV: TERMINAL_ENV || "MAIN"
  },
  dva: {
    hmr: true,
  },
  dynamicImport: {
    loading: '@/components/PageLoading/index',
  },
  esbuild: {},
  fastRefresh: {},
  hash: true,
  history: {
    type: 'hash',
  },
  ignoreMomentLocale: true,
  inlineLimit: 3000,
  locale: {
    default: 'zh-CN',
    antd: true,
    baseNavigator: true,
  },
  manifest: {
    basePath: '/',
  },
  mountElementId: 'root-master',
  // umi routes: https://umijs.org/docs/routing
  routes,
  targets: {
    ie: 11,
  },
  title: false,
  proxy: proxy,
  qiankun: MICRO_CIG,
  webpack5: {},
  alias: {
    "#": '/src/tool'
  },
});

